import Cookies from "js-cookie";
export function param2Obj(url) {
  const search = decodeURIComponent(url.split("?")[1]).replace(/\+/g, " ");
  if (!search) {
    return {};
  }
  const obj = {};
  const searchArr = search.split("&");
  searchArr.forEach((v) => {
    const index = v.indexOf("=");
    if (index !== -1) {
      const name = v.substring(0, index);
      const val = v.substring(index + 1);
      obj[name] = val;
    }
  });
}

export const downloadFile = (url, query) => {
  const aLink = window.document.createElement('a');
  const evt = window.document.createEvent('MouseEvents');
  const req = {}
  Object.keys(query).forEach(key => {
    if (query[key]) req[key] = query[key]
  })
  let str = '';
  str = `${import.meta.env.VITE_APP_BASE_API}${url}?uid=${Cookies.get('uid')}&${Object.keys(req)
    .map(key => {
      return `${encodeURIComponent(key)}=${encodeURIComponent(req[key])}`;
    })
    .join('&')}`;
  evt.initEvent('click', true, true);
  aLink.href = str;
  aLink.dispatchEvent(evt);
};